﻿using RoadQualityEvaluatorData.Domain.Entities;
using RoadQualityEvaluatorData.DomainServices.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RoadQualityEvaluatorData.DomainServices.Repositories
{
    public class UserRepository : Repository<User>
    {

        private RoleRepository _rolesRepo;

        public UserRepository()
            : base()
        {
            
        }

        public UserRepository(UnitOfWork uo)
            : base(uo)
        {
        }


        public User GetUserByUsername(string username)
        {
            try
            {
                return this.GetAll().FirstOrDefault(u => u.Username.Equals(username));
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public void AddExistingRoleToUser(int roleId, User user)
        {
            this._rolesRepo = new RoleRepository();
            var role = _rolesRepo.GetById(roleId);

            if (role != null)
            {
                user.UserRoles.Add(role);
                this.Update(user);
            }
            
        }

        public void AddExistingRoleToUser(string roleName, User user)
        {
            this._rolesRepo = new RoleRepository();
            var role = _rolesRepo.GetRoleByName(roleName);

            if (role != null)
            {
                user.UserRoles.Add(role);
                this.Update(user);
            }

        }

    }
}
